<?php


class AuthCtrl extends Ctrl{
	
	public function logout() {
	
		setcookie(COOKUSER, '', (time()-3600), '/','',0);
		setcookie(COOKTOKEN, '', (time()-3600), '/','',0);
	
		$this->pdio->close();
		$this->delorean->setRecord(false);
		$this->delorean->reloadRefresh();
	
	}
	
	public function loginform() {
		
		
		$this->response->setTpl('/home/valerio/www/vfw/plugins/Auth/view/loginForm.html');
		
	}
	
	public function login() {
		
		$this->delorean->setRecord(false);

		$auth = $this->plugins['Auth'];
		$this->getError($errorMsg, true);
		
		$form = $this->FormLogin();
		$form->readRequest($this->request);
		$validation = $form->validate();
		
		if (!empty($validation)) {
			$this->goBack("errori validazione:<br>".print_r($validation, true));
		} else {
			$this->response->set('testo', "validazione ok");
		}
		
		$res = $auth->login($this->request->get('uname'), $this->request->get('pass'));
		
		$msglogin = 'Login ok';
		if (!$res) {
			$msglogin = 'Login errato';
		}
		
		$this->delorean->goBack($msglogin);
		
		
	}
	
	private function FormLogin()  {
	
		$form = new FormHandler();
	
		$form->addField('uname', DataTypes::ALFANUMERICO, true);
		$form->addField('pass', DataTypes::ALFANUMERICO, true);
	
		return $form;
	
	}	
	
}